package edu.zjut.sjq.controller;

import edu.zjut.sjq.bean.Grade;
import edu.zjut.sjq.bean.Result;
import edu.zjut.sjq.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * Created by sue on 2017/3/18.
 */
@Controller
@RequestMapping("/grade")
public class GradeController {
    @Autowired
    private GradeService gradeService;

    /**
     * 评分
     * @param grade
     * @return
     */
    @RequestMapping(value = "/add",produces="application/json")
    @ResponseBody
    public Result addGrade(@RequestBody Grade grade){
        return gradeService.addGrade(grade);
    }

    //删除评分
    @RequestMapping(value = "/delete",produces="application/json",method = RequestMethod.GET)
    @ResponseBody
    public Result deleteGrade(@RequestParam("articleId") Integer articleId){
        return gradeService.deleteGrade(articleId);
    }


    /**
     * 获取该作品的所有评分
     * @param articleId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAll")
    public List<Grade> getAllGrade(@RequestParam Integer articleId){
        return gradeService.getAllGrade(articleId);
    }

    /**
     * 统计平均分
     * @param articleId
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping("/countAvg")
    public Number countGradeAvg(@RequestParam Integer articleId) throws ParseException {
        return gradeService.countGradeAvg(articleId);
    }
}
